package www.sofn.com.accounts;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.CircleCaptcha;
import cn.hutool.core.util.IdUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import www.sofn.com.accounts.domain.CaptchaVO;
import www.sofn.com.accounts.domain.LoginInfoVO;
import www.sofn.com.constant.Constants;
import www.sofn.com.domain.R;
import www.sofn.com.domain.vo.PersonalInfoVo;

import java.util.concurrent.TimeUnit;

@RestController
@Tag(name = "账号管理")
public class AccountController {

    @Resource
    private AccountsService accountsService;
    @Resource
    private RedisTemplate<String, String> redisTemplate;

    @PostMapping("/do_login")
    @Operation(summary = "登录")
    public R<String> doLogin(@RequestBody @Validated LoginInfoVO loginInfoVO) {
        return R.ok(accountsService.doLogin(loginInfoVO));
    }

    @GetMapping("/get_captcha")
    @Operation(summary = "获取验证码")
    public R<CaptchaVO> getCaptcha() {
        CircleCaptcha captcha = CaptchaUtil.createCircleCaptcha(200, 40, 5, 3);
        //设置颜色
        //captcha.setBackground(new Color(34, 104, 63));
        CaptchaVO captchaVO = new CaptchaVO(IdUtil.fastSimpleUUID(), captcha.getImageBase64Data());
        redisTemplate.opsForValue().set(Constants.CAPTCHA_PREFIX + captchaVO.getId(), captcha.getCode(), 5, TimeUnit.MINUTES);
        return R.ok(captchaVO);
    }

    @GetMapping("personal_info")
    @Operation(summary = "用户信息")
    public R<PersonalInfoVo> getPersonal() {
        return R.ok(accountsService.getPersonal());
    }
}
